﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Aribaccio.Device
{
    /// <summary>
    /// 设备基类
    /// </summary>
    /// <typeparam name="TPara">设备链接参数接口</typeparam>
    public class DeviceBase<TPara> : IDevice<TPara> where TPara : IDevicePara
    {
        /// <summary>
        /// 定义一个标识确保线程同步
        /// </summary>
        protected readonly object Locker = new object();
        /// <summary>
        /// 设备是否被打开[虚方法重写]
        /// </summary>
        public virtual bool IsOpen { get; }
        /// <summary>
        /// 设备是否被使用
        /// </summary>
        public bool IsUsed { get; set; }
        /// <summary>
        /// 设备地址端口
        /// </summary>
        public virtual string AddressPort => string.Empty;
        /// <summary>
        /// 设备参数接口对象
        /// </summary>
        public TPara DevicePara { get; set; }
        /// <summary>
        /// 数据交互的锁
        /// </summary>
        public SimpleHybirdLock HybirdLock { get; set; }
        /// <summary>
        /// 存储设备数据集合
        /// </summary>
        public List<IDeviceTrans<IDeviceData<IDevice<IDevicePara>>>> DeviceDataNode { get; set; }
        
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="argIDevicePara">设备参数接口对象</param>
        public DeviceBase(TPara argIDevicePara)
        {
            this.DevicePara = argIDevicePara;
            this.HybirdLock = new SimpleHybirdLock();
            this.DeviceDataNode = new List<IDeviceTrans<IDeviceData<IDevice<IDevicePara>>>>();
        }

        /// <summary>
        /// 打开设备连接[虚方法重写]
        /// </summary>
        /// <returns>是否成功信息</returns>
        public virtual Result Open() { return new Result(); }

        /// <summary>
        /// 关闭设备连接[虚方法重写]
        /// </summary>
        /// <returns>是否成功信息</returns>
        public virtual Result Close() { return new Result(); }

        /// <summary>
        /// 绑定设备连接
        /// </summary>
        /// <param name="argDeviceTransData">通道传输数据接口</param>
        /// <returns></returns>
        public IDevice<TPara> BindingDevice(IDeviceTrans<IDeviceData<IDevice<IDevicePara>>> argDeviceTransData)
        {
            if (argDeviceTransData == null || this.DeviceDataNode == null) { return null; }
            if (!this.DeviceDataNode.Exists(D => D.GUID == argDeviceTransData.GUID))
            {
                this.DeviceDataNode.Add(argDeviceTransData);
            }
            this.IsUsed = true;
            return this;
        }
      
        /// <summary>
        /// 绑定设备清除
        /// </summary>
        /// <param name="argDeviceTransData">通道传输数据接口</param>
        /// <returns></returns>
        public void ClearBindingDevice(IDeviceTrans<IDeviceData<IDevice<IDevicePara>>> argDeviceTransData)
        {
            if (argDeviceTransData == null || this.DeviceDataNode == null) { return; }
            this.DeviceDataNode.RemoveAll(D => D.GUID == argDeviceTransData.GUID);
            this.IsUsed = false;
        }
    }
}
